package com.kotlin.demo.net

import okhttp3.FormBody
import okhttp3.RequestBody
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
import java.net.HttpURLConnection
import java.net.URL
import java.net.URLEncoder
import java.nio.charset.StandardCharsets


/**
 * 翻译接口 列表
 */
object ApiTranslateOk {
    val apiUrl = "http://10.64.12.17"

    //好用的免费API
    //https://suggest.taobao.com/sug?code=utf-8&q=man&callback=cb
    //https://api.uomg.com/api/long2dwz
    @JvmStatic
    fun main(args: Array<String>) {
        val data = "f=auto&t=auto&w=" + URLEncoder.encode("你好", StandardCharsets.UTF_8)
        val url = "http://fy.iciba.com/ajax.php?a=fy&$data"
        println(url)
//        try {
//            val document: Document = Jsoup.connect(url).get()
//            println(document.body())
//            val elements: Elements = document.select(".base-dict")
//            for (element in elements) {
//                println(element.text())
//            }
//        } catch (e: IOException) {
//            e.printStackTrace()
//        }

//        val url = "http://fy.iciba.com/ajax.php?a=fy"
//        val data = "f=auto&t=auto&w=" + URLEncoder.encode("你好", StandardCharsets.UTF_8)
//
//        val connection = URL(url).openConnection() as HttpURLConnection
//        connection.requestMethod = "POST"
//        connection.doOutput = true
//        connection.outputStream.write(data.toByteArray(StandardCharsets.UTF_8))
//
//        val reader = BufferedReader(InputStreamReader(connection.inputStream, StandardCharsets.UTF_8))
//        val response = StringBuilder()
//        var line: String?
//        while (reader.readLine().also { line = it } != null) {
//            response.append(line)
//        }
//        reader.close()
//
//        println(response.toString())

    }

    fun getTranslateTongYi(translate: String, netBack: NetBack?) {
        val response: StringBuilder
        try {
            val content = URLEncoder.encode(translate)
            val obj = URL("${apiUrl}/translate_tongyi?name=${content}")
            val con = obj.openConnection() as HttpURLConnection

            // 设置请求类型为GET
            con.requestMethod = "GET"
            val responseCode = con.responseCode
            println("Response Code : $responseCode")

            // 读取响应
            val `in` = BufferedReader(InputStreamReader(con.inputStream))
            var inputLine: String?
            response = StringBuilder()
            while (`in`.readLine().also { inputLine = it } != null) {
                response.append(inputLine)
            }
            `in`.close()
            netBack?.back(response.toString())
            // 打印结果
            println(response.toString())
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun getTranslateBaidu(translate: String, netBack: NetBack?) {
        val response: StringBuilder
        try {
            val content = URLEncoder.encode(translate)
            val obj = URL("${apiUrl}/translate_baidu?name=${content}")
            val con = obj.openConnection() as HttpURLConnection

            // 设置请求类型为GET
            con.requestMethod = "GET"
            val responseCode = con.responseCode
            println("Response Code : $responseCode")

            // 读取响应
            val `in` = BufferedReader(InputStreamReader(con.inputStream))
            var inputLine: String?
            response = StringBuilder()
            while (`in`.readLine().also { inputLine = it } != null) {
                response.append(inputLine)
            }
            `in`.close()
            netBack?.back(response.toString())
            // 打印结果
            println(response.toString())
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 爱词霸翻译
     */
    fun getTranslateIcibaByOkHttp(translate: String, netBack: NetBack?) {
        val requestBody: RequestBody = FormBody.Builder()
            .add("code", "").build()
        OkHttpUtil.post("${apiUrl}/translate_iciba?name=${translate}", requestBody, object :
            OkHttpUtil.HttpCallBack {
            override fun callBack(response: String?) {
                if (response != null) {
                    netBack?.back(response.toString())
                } else {
                    netBack?.back("翻译出错")
                }
            }

        })
    }

    /**
     * 金山翻译
     */
    fun getTranslateByJinShanOkHttp(translate: String, netBack: NetBack?) {
        val requestBody: RequestBody = FormBody.Builder()
            .add("code", "").build()
        OkHttpUtil.post("${apiUrl}/translate_jin_shan?name=${translate}", requestBody, object :
            OkHttpUtil.HttpCallBack {
            override fun callBack(response: String?) {
                if (response != null) {
                    netBack?.back(response.toString())
                } else {
                    netBack?.back("翻译出错")
                }
            }

        })
    }
    /**
     * 百度翻译
     */
    fun getTranslateBaiduByOkHttp(translate: String, netBack: NetBack?) {
        val requestBody: RequestBody = FormBody.Builder()
            .add("code", "").build()
        OkHttpUtil.post("${apiUrl}/translate_baidu?name=${translate}", requestBody, object :
            OkHttpUtil.HttpCallBack {
            override fun callBack(response: String?) {
                if (response != null) {
                    netBack?.back(response.toString())
                } else {
                    netBack?.back("翻译出错")
                }
            }

        })
    }

    /**
     * 谷歌翻译
     */
    fun getTranslateGoogleByOkHttp(translate: String, netBack: NetBack?) {
        val requestBody: RequestBody = FormBody.Builder()
            .add("code", "").build()
        OkHttpUtil.post("${apiUrl}/translate_google?name=${translate}", requestBody, object :
            OkHttpUtil.HttpCallBack {
            override fun callBack(response: String?) {
                if (response != null) {
                    netBack?.back(response.toString())
                } else {
                    netBack?.back("翻译出错")
                }
            }

        })
    }
}